import os
import re
import glob

base_dir = '../analysis/'  # Change as needed or make configurable
summary_path = os.path.join(base_dir, 'clustering_summary.md')

rows = []

for subdir in sorted(os.listdir(base_dir)):
    analysis_dir = os.path.join(base_dir, subdir)
    if not os.path.isdir(analysis_dir):
        continue
    dom_path = os.path.join(analysis_dir, 'dominant_families_report.txt')
    sil_path = os.path.join(analysis_dir, 'silhouette_scores.csv')
    if not (os.path.exists(dom_path) and os.path.exists(sil_path)):
        continue
    # Parse feature combination from directory name
    feature_combo = subdir
    # Parse dominant families report
    with open(dom_path) as f:
        lines = f.readlines()
    total_families = dominant_families = ratio = None
    for line in lines:
        if line.startswith('Total families:'):
            total_families = int(line.split(':')[1].strip())
        elif line.startswith('Dominant families:'):
            dominant_families = int(line.split(':')[1].strip())
        elif line.startswith('Ratio:'):
            ratio = float(line.split(':')[1].strip())
    # Parse silhouette scores
    best_k = None
    best_score = -1
    with open(sil_path) as f:
        next(f)  # skip header
        for row in f:
            k, score = row.strip().split(',')
            k = int(k)
            score = float(score)
            if score > best_score:
                best_score = score
                best_k = k
    rows.append((feature_combo, best_k, best_score, total_families, dominant_families, ratio))

# Sort by silhouette score descending
rows.sort(key=lambda x: x[2], reverse=True)

with open(summary_path, 'w') as f:
    f.write('# Clustering Summary Report\n\n')
    f.write('| Feature Combination | Best K | Best Silhouette Score | Total Families | Dominant Families | Ratio |\n')
    f.write('|---------------------|--------|----------------------|----------------|-------------------|-------|\n')
    for combo, k, score, total, dom, ratio in rows:
        f.write(f'| {combo} | {k} | {score:.4f} | {total} | {dom} | {ratio:.4f} |\n')
print(f"Summary written to {summary_path}") 